﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ATEC.ATDataCoreNET;
using ATEC.GeometryNET;
using SSDPNET;
//using SSDPNET.SZEWObject;
using SSDPNET.Geom;
using ATEC.ATDataCoreNET.ATObjects;
using SZEWAlignmentNet.AlignmentBaseClass;
using SZEWAlignmentNet.AlignmentInterface;
using SZEWAlignmentNet.SpatialAlignmentInfo;
using SZEWAlignmentNet.HorizontalAlignmentInfo;
using SZEWAlignmentNet.VerticalAlignmentInfo;
using SZEWAlignmentNet.AlignmentCrossSectionInfo;
using static System.Collections.Specialized.BitVector32;

namespace SZEWAlignmentUtilExample
{
    public enum ScenceType
    {
        SpatialScene = 0,//空间模型
        HorizonScene = 1,//平面模型
        VerticalScene = 2,//纵断面模型
    };

    public partial class AlignmentTestForm : Form
    {
        private void AddMeshToScene(PolyfaceMeshObject meshIn, int color, bool IsToSpatialSence = true)
        {
            //获取当前激活Scene
            ATSceneRef sceneActive = SSDPNET.ScenesFunctions.SceneRefGetActive();
            ATProject propject = sceneActive.GetATProject();
            ATSceneRef scene;
            if (IsToSpatialSence)
                scene = SZEWAlignmentInterface.GetSpatialScene(propject);
            else
                scene = SZEWAlignmentInterface.GetHoriScene(propject);

            ATEC.ATDataCoreNET.ATObjects.ATObject obj =
                ATEC.ATDataCoreNET.ATObjects.GeometryToATObject.ToObject(scene.GetATScene(), meshIn, null);
            ObjectPropertiesSetter setter = new ObjectPropertiesSetter();
            setter.SetColor((uint)color);
            setter.Apply(obj);
            obj.AddToScene();
        }

        private void AddPointToScene(XPoint3d origionPt, double rd, int color, ScenceType sceneType = ScenceType.SpatialScene)
        {
            ATSphereDetail detail2 = new ATSphereDetail(origionPt, rd);
            BaseSolid sphere = BaseSolid.CreateATSphere(detail2);
            PolyfaceMeshObject sphereMesh = IPolyfaceConstructionExtention.CreateFromBaseSolid(sphere);

            ATSceneRef sceneActive = SSDPNET.ScenesFunctions.SceneRefGetActive();
            ATProject propject = sceneActive.GetATProject();
            ATSceneRef scene;
            if (ScenceType.HorizonScene == sceneType)
                scene = SZEWAlignmentInterface.GetHoriScene(propject); 
            else if (ScenceType.VerticalScene == sceneType)
                scene = SZEWAlignmentInterface.GetVerticalScene(propject);
            else
                scene = SZEWAlignmentInterface.GetSpatialScene(propject);

            ATEC.ATDataCoreNET.ATObjects.ATObject obj =
                ATEC.ATDataCoreNET.ATObjects.GeometryToATObject.ToObject(scene.GetATScene(), sphereMesh, null);
            ObjectPropertiesSetter setter = new ObjectPropertiesSetter();
            setter.SetColor((uint)color);
            setter.Apply(obj);
            obj.AddToScene();
        }

        private void AddCrveToScene(CurveSet crvIn, int color, ScenceType sceneType = ScenceType.SpatialScene)
        {
            //获取当前激活Scene
            ATSceneRef sceneActive = SSDPNET.ScenesFunctions.SceneRefGetActive();
            ATProject propject = sceneActive.GetATProject();
            ATSceneRef scene;
            if (ScenceType.HorizonScene == sceneType)
                scene = SZEWAlignmentInterface.GetHoriScene(propject);
            else if (ScenceType.VerticalScene == sceneType)
                scene = SZEWAlignmentInterface.GetVerticalScene(propject);
            else
                scene = SZEWAlignmentInterface.GetSpatialScene(propject);

            ATEC.ATDataCoreNET.ATObjects.ATObject obj =
                ATEC.ATDataCoreNET.ATObjects.GeometryToATObject.ToObject(scene.GetATScene(), crvIn, null);
            ObjectPropertiesSetter setter = new ObjectPropertiesSetter();
            setter.SetColor((uint)color);
            setter.Apply(obj);
            obj.AddToScene();
        }

        private void DrawCrossSection(CrossSectionInfo info,int color)
        {
            foreach(FeatureObjectsInfo elemInfo in info.FeatureObjects)
            {
                XPoint3d leftPt = elemInfo.StartPt;
                XPoint3d rightPt = elemInfo.EndPt;

                AddPointToScene(leftPt, 10000, 3);
                AddPointToScene(rightPt, 10000, 3);
            }
        }

        public AlignmentTestForm()
        {
            InitializeComponent();
        }

        private void button_QueryAllAlign_Click(object sender, EventArgs e)
        {
            // 路线信息类 AlignmentInfo
            List<AlignmentInfo> allInfo = new List<AlignmentInfo>();

            // 查找所有路线信息
            if (!SZEWAlignmentInterface.GetAllAlignmentInfo(allInfo))
                return;

            string mess = "";
            for(int aligIndex = 0; aligIndex < allInfo.Count; aligIndex++)
            {
                // 获取名称、桩号等信息
                string singleInfo = string.Format("id: {0} ; 名称: {1} ;起始桩号：{2}  ; 终止桩号： {3} \n",
                   allInfo[aligIndex].Id, allInfo[aligIndex].Name, allInfo[aligIndex].StartStation, allInfo[aligIndex].EndStation);
                mess += singleInfo;
            }

            MessageBox.Show(mess);
        }

        private bool GetFirstAlignmentInfo(out AlignmentInfo info)
        {
            info = null;
            List<AlignmentInfo> allInfo = new List<AlignmentInfo>();
            // 查找所有路线
            if (!SZEWAlignmentInterface.GetAllAlignmentInfo(allInfo))
                return false;
            if (allInfo.Count == 0)
                return false;

            // 取第一条路线，转为 ATObject，模拟鼠标选中的结果
            ATObject obj = allInfo[0].Scene.FindObjectById(allInfo[0].Id);
            if (obj == null)
                return false;

            // 从指定 ATObject 查询路线信息
            if (!SZEWAlignmentInterface.QueryObjectAlignmentInfo(out info, obj))
                return false;

            return true;
        }

        private void button_QuerySelected_Click(object sender, EventArgs e)
        {
            // 选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            // 查询路线信息
            string singleInfo = string.Format("id: {0} ; 名称: {1} ;起始桩号：{2}  ; 终止桩号： {3}",
                 info.Id, info.Name, info.StartStation, info.EndStation);

            MessageBox.Show(singleInfo);

        }

        private void button_QueryCrossSection_Click(object sender, EventArgs e)
        {
            IDStationInput form = new IDStationInput();
            form.ShowDialog();
            if (form.result != DialogResult.OK)
                return;

            // 取用户输入的桩号
            string station = "";
            if (!form.GetStation(ref station))
                return;

            // 取第一条路线
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            // 查询桩号位置处路线横断面信息
            CrossSectionInfo crossSectionInfo = new CrossSectionInfo();
            if (!alignmentInterface.GetCrossSectionByStation(crossSectionInfo, station))
            {
                MessageBox.Show("查询失败");
                return;
            }
            DrawCrossSection(crossSectionInfo, 3);  // 绘制横断面点

            MessageBox.Show("已在桩号处生成横断面点");
        }

        private void button_FindStationInAlignment_Click(object sender, EventArgs e)
        {
            //获取第一条路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //给定路线id和空间点，计算所在路线桩号
            string dStation = "";
            XPoint3d spacePt = XPoint3d.FromXYZ(0, 0, 0);
            if (!alignmentInterface.FindStationInAlignment(out dStation, spacePt))
            {
                MessageBox.Show("空间点在路线上投影失败");
                return;
            }

            MessageBox.Show("(0,0,0)空间点在路线上投影处桩号为：" + dStation);
        }

        private void button_GetClosestAndPoint_Click(object sender, EventArgs e)
        {
            //获取第一条路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询平面上某点在平面线上的最近点及最近点的原始桩号
            XPoint2d planePt = XPoint2d.FromXY(0, 0);
            string dStation = "";//桩号
            XPoint2d ptOnAlign;//投影点
            if (!alignmentInterface.GetClosestAndPointStation(out ptOnAlign, out dStation, planePt))
            {
                MessageBox.Show("目标点在平面线上最近点获取失败");
                return;
            }

            MessageBox.Show("(0,0)平面点在平面线上最近点原始桩号为：" + dStation);
        }

        private void button_QueryRoadbedType_Click(object sender, EventArgs e)
        {
            //获取第一条路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询指定桩号范围的路基类型
            string startStation = "K0+400";
            string endStation = "K0+800";
            string roadbedType = "";
            if (!alignmentInterface.QueryRoadbedType(out roadbedType, startStation, endStation))
            { 
                MessageBox.Show("路基类型获取失败");
                return;
            }

            MessageBox.Show(startStation + "-" + endStation + "桩号范围路基类型为：" + roadbedType);
        }

        private void button_QueryDetailByStation_Click(object sender, EventArgs e)
        {
            IDStationInput form = new IDStationInput();
            form.ShowDialog();
            if (form.result != DialogResult.OK)
                return;

            // 输入目标桩号
            string station = "";
            if (!form.GetStation(ref station))
                return;

            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //获取桩号处路线详细信息
            LocationDetailInfo detailInfo = new LocationDetailInfo();
            if (!alignmentInterface.GetLocationDetailByStation(detailInfo, station) || detailInfo == null)
            {
                MessageBox.Show("路线详细信息获取失败");
                return;
            }

            //绘制空间点
            AddPointToScene(detailInfo.point, 100000, 3);
            MessageBox.Show("路线详细信息获取成功, 已在桩号处生成空间点");
        }

        private void button_QuerySptailCurveByRange_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //获取桩号范围的空间线
            string startStation = "K0+400";//起点桩号
            string endStation = "K0+800";//终点桩号
            double offset = 100;//偏移值
            CurveSet sptailCurve = null;
            if (!alignmentInterface.GetSpatialCurveByStationRange(out sptailCurve, startStation, endStation, offset) || sptailCurve == null)
            {
                MessageBox.Show("空间线获取失败");
                return;
            }

            //绘制空间线
            AddCrveToScene(sptailCurve, 3);
            MessageBox.Show("空间线绘制成功");
        }

        private void button_QueryFeatureCurveByRange_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //获取给定桩号范围的特征线
            string startStation = "K0+400";//起点桩号
            string endStation = "K0+800";//终点桩号
            CrossSectionFeatureObjectType type = CrossSectionFeatureObjectType.Lane;
            CrossSectionFeatureObjectPosition pos = CrossSectionFeatureObjectPosition.Left;
            List<Tuple<CrossSectionFeatureObjectType, CurveSet>> fearureCrvs = new List<Tuple<CrossSectionFeatureObjectType, CurveSet>>();
            if (!alignmentInterface.ExtractAllCurve(fearureCrvs, startStation, endStation) || 0 == fearureCrvs.Count)
            {
                MessageBox.Show("特征线获取失败");
                return;
            }

            //绘制行车道特征线
            foreach (Tuple<CrossSectionFeatureObjectType, CurveSet> singleTypeCrv in fearureCrvs)
            {
                if (singleTypeCrv.Item1 == CrossSectionFeatureObjectType.Lane)
                    AddCrveToScene(singleTypeCrv.Item2, 3);
            }

            MessageBox.Show("特征线绘制成功");
        }

        private void button_QueryCrossSectionInfoByStation_Click(object sender, EventArgs e)
        {
            IDStationInput form = new IDStationInput();
            form.ShowDialog();
            if (form.result != DialogResult.OK)
                return;

            // 输入目标桩号
            string station = "";
            if (!form.GetStation(ref station))
                return;

            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //获取桩号处横断面信息
            CrossSectionInfo crossSectionInfo = new CrossSectionInfo();
            if (!alignmentInterface.GetCrossSectionByStation(crossSectionInfo, station) || crossSectionInfo == null)
            {
                MessageBox.Show("横断面信息获取失败");
                return;
            }

            //绘制横断面处空间点
            AddPointToScene(crossSectionInfo.PtInAlig, 100000, 3);
            MessageBox.Show("横断面信息获取成功, 已在桩号处生成空间点");
        }

        private void button_QueryRoadBorder_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询桩号范围路面边界
            string startStation = "K0+400";//起点桩号
            string endStation = "K0+800";//终点桩号
            double offset = 100;//偏移值
            CurveSet leftBorder = null;//左边界
            CurveSet rightBorder = null;//右边界
            if (!alignmentInterface.CollectBorders(out leftBorder, out rightBorder, startStation, endStation, offset) || leftBorder == null || rightBorder == null)
            {
                MessageBox.Show("路面边界获取失败");
                return;
            }

            //在平面模型中绘制路面边界线
            AddCrveToScene(leftBorder, 3, ScenceType.HorizonScene);
            AddCrveToScene(rightBorder, 3, ScenceType.HorizonScene);
            MessageBox.Show("路面边界绘制成功");
        }

        private void button_QueryRoadFeaturePtsByStaion_Click(object sender, EventArgs e)
        {
            IDStationInput form = new IDStationInput();
            form.ShowDialog();
            if (form.result != DialogResult.OK)
                return;

            // 输入目标桩号
            string station = "";
            if (!form.GetStation(ref station))
                return;

            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询特征点信息
            List<Tuple<CrossSectionFeatureObjectType, List<XPoint3d>>> featurePtsMap = new List<Tuple<CrossSectionFeatureObjectType, List<XPoint3d>>>();
            double epsion = 0.001;//浮点数比较容差
            if (!alignmentInterface.GetAllRoadPtsAtStation(featurePtsMap, station, epsion, true))
            {
                MessageBox.Show("路线特征点查询失败");
                return;
            }

            //绘制行车道处的特征点
            foreach (Tuple<CrossSectionFeatureObjectType, List<XPoint3d>> singlePtsMap in featurePtsMap)
            {
                if (singlePtsMap.Item1 == CrossSectionFeatureObjectType.Lane)
                { 
                    foreach(XPoint3d pt in singlePtsMap.Item2)
                        AddPointToScene(pt, 10000, 3);
                }
            }

            MessageBox.Show("已在行车道处生成特征点, 路线特征点查询成功");
        }

        private void button_QueryHorizonCurve_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询路线平曲线
            string startStationStr = "K1+000 ";//起点桩号
            string endStationStr = "K2+800";//终点桩号
            //CurveSet horizontalCrv = CurveSet.Create(CurveSet.BoundaryType.Open);
            CurveSet horizontalCrv = null;
            if (!alignmentInterface.GetHorizontalCurve(out horizontalCrv, startStationStr, endStationStr) || horizontalCrv == null)
            {
                MessageBox.Show("路线平曲线获取失败");
                return;
            }

            //在平面模型中绘制平曲线
            AddCrveToScene(horizontalCrv, 2, ScenceType.HorizonScene);
            MessageBox.Show("路线平曲线绘制成功");
        }

        private void button_QueryControlPoints_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询平面线交点集
            List<SzewTraverseControlPoint> controlPoints = new List<SzewTraverseControlPoint>();
            if (!alignmentInterface.GetControlPoints(controlPoints) || 0 == controlPoints.Count)
            {
                MessageBox.Show("控制点获取失败");
                return;
            }

            //绘制平面线交点
            foreach (SzewTraverseControlPoint contolPt in controlPoints)
                AddPointToScene(contolPt.m_point, 100000, 3, ScenceType.HorizonScene);

            MessageBox.Show("控制点绘制成功");
        }

        private void button_QueryPlanePointByDist_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询平面线指定距离处平面点及切向量（单位化）
            XPoint2d plantPt;
            XVector2d tangent;
            double dist = 1500000;//指定距离
            if (!alignmentInterface.CalculatePlanePointOfDist(out plantPt, out tangent, dist))
            {
                MessageBox.Show("查询失败");
                return;
            }

            //绘制平面点及切向量
            XPoint3d drawPt=new XPoint3d(plantPt);
            XVector3d drawVec = new XVector3d(tangent);
            List<XPoint3d> pts = new List<XPoint3d> { drawPt, drawPt + drawVec * 10000000 };
            CurveSet tangentCrv = CurveSet.CreateLinear(pts, CurveSet.BoundaryType.Open, true);
            AddPointToScene(drawPt, 100000, 3, ScenceType.HorizonScene);
            AddCrveToScene(tangentCrv, 2, ScenceType.HorizonScene); 
            MessageBox.Show("平面点及切向量绘制成功");
        }

        private void button_QueryIntersecInfoByHoriAlign_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            // 模拟选择第二条路线
            List<AlignmentInfo> allInfo = new List<AlignmentInfo>();
            if (!SZEWAlignmentInterface.GetAllAlignmentInfo(allInfo))
                return;
            if (allInfo.Count < 2)
                return;
            AlignmentInfo secondInfo = allInfo[1];

            //查询两路线平面交叉点信息
            XPoint2d intersectPt = new XPoint2d();
            double elevation = 0;
            string dStation = "K1+000";
            if (!alignmentInterface.GetHorizontalIntersectionInfo(out intersectPt, elevation, dStation, secondInfo))
            {
                MessageBox.Show("查询失败");
                return;
            }

            //在平面模型中绘制交叉点
            AddPointToScene(intersectPt, 100000, 3, ScenceType.HorizonScene);
            MessageBox.Show("交叉点绘制成功");
        }

        private void button_QueryVerticalStations_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询纵断面要素桩号，此接口功能暂未实现，todo...
            List<string> segInfos = new List<string>();
            if (!alignmentInterface.GetVeticalEssentialStations(segInfos) || 0 == segInfos.Count)
            {
                MessageBox.Show("查询失败");
                return;
            }

            string msg = "查询成功, 要素桩号为：";
            foreach (string station in segInfos)
            {
                msg += "\n";
                msg += station;
            }
            MessageBox.Show(msg);
        }

        private void button_QueryElevationByStaion_Click(object sender, EventArgs e)
        {
            IDStationInput form = new IDStationInput();
            form.ShowDialog();
            if (form.result != DialogResult.OK)
                return;

            // 输入目标桩号
            string station = "";
            if (!form.GetStation(ref station))
                return;

            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询桩号位置高程信息
            double elevation = 0;//高程
            double startStationOfHoriAlign = 0;//平面线起点绝对桩号值；
            double dist;//根据输入桩号计算到平面线起点距离
            if (!alignmentInterface.TransformEquationStationToOriginalStation(out dist, station))
                return;
            double epsion = 0.001;//浮点数比较容差
            int segmentIndex = 0;//指定从第几个线元开始搜索
            //该接口返回纵断面线元索引，计算失败返回-1
            if (-1 == alignmentInterface.CalculateElevationForStation(out elevation, startStationOfHoriAlign, dist, epsion, segmentIndex))
            {
                MessageBox.Show("高程信息查询失败");
                return;
            }

            MessageBox.Show("高程信息为：" + elevation.ToString());
        }

        private void button_QueryVerticalCurve_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询纵断面线
            CurveSet elevationCrv = null;
            double startStationOfHoriAlign = 10000;//对应平面线起点桩号；
            bool setRatio = false;
            double ratio = 1;
            if (!alignmentInterface.GetGeometry(out elevationCrv, startStationOfHoriAlign, setRatio, ratio) || elevationCrv == null)
            {
                MessageBox.Show("纵断面线查询失败");
                return;
            }

            //在纵断面模型中绘制纵断面线
            AddCrveToScene(elevationCrv, 2, ScenceType.VerticalScene);

            MessageBox.Show("纵断面线绘制成功");
        }

        private void button_QueryAlignmentDesignInfo_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询路线设计信息
            AlignmentDesignInfo alignDesignInfo = new AlignmentDesignInfo();
            if (!alignmentInterface.GetAlignmentDesignInfo(alignDesignInfo))
            {
                MessageBox.Show("路线设计信息查询失败");
                return;
            }

            MessageBox.Show("路线设计信息查询成功");
        }

        private void button_QueryWidthData_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询宽度
            List<CrossSectionInfo> widthDatas = new List<CrossSectionInfo>();
            if (!alignmentInterface.GetCrossSectionWidthData(widthDatas) || widthDatas.Count == 0)
            {
                MessageBox.Show("宽度查询失败");
                return;
            }

            MessageBox.Show("宽度查询成功");
        }

        private void button_QueryLeftSideSegmentsInfo_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询左侧填挖方信息
            List<SideSlopeSegmentInfo> sideSlopeSegmentInfos = new List<SideSlopeSegmentInfo>();
            if (!alignmentInterface.GetLeftSideSlopeSegments(sideSlopeSegmentInfos) || sideSlopeSegmentInfos.Count == 0)
            {
                MessageBox.Show("左侧填挖方信息查询失败");
                return;
            }

            MessageBox.Show("左侧填挖方信息查询成功");
        }

        private void button_QueryVerticalSegment_Click(object sender, EventArgs e)
        {
            //选取第一条路线，模拟鼠标选中的结果，获取路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //查询纵断面线元信息
            List<SZEWVerticalSegmentInfo> verticalSegmentInfos = new List<SZEWVerticalSegmentInfo>();
            if (!alignmentInterface.GetVerticalSegmentInfos(verticalSegmentInfos) || verticalSegmentInfos.Count == 0)
            {
                MessageBox.Show("纵断面线元查询失败");
                return;
            }

            MessageBox.Show("纵断面线元查询成功");
        }

        private void button_Test_Click(object sender, EventArgs e)
        {
            //获取第一条路线信息
            AlignmentInfo info;
            if (!GetFirstAlignmentInfo(out info))
                return;

            //初始化Inerface接口
            SZEWAlignmentInterface alignmentInterface = new SZEWAlignmentInterface(info);

            //断链桩号转为原始桩号
            {
                double originalStation = 0;
                string equationStation = "K5+000";
                if (!alignmentInterface.TransformEquationStationToOriginalStation(out originalStation, equationStation))
                    MessageBox.Show("断链桩号转为原始桩号 fail");
                else
                    MessageBox.Show("断链桩号转为原始桩号 ok");
            }

            //原始桩号转为断链桩号
            {
                string equationStationStr = "";
                double originalStation = 1000000;
                if (!alignmentInterface.TransformOriginalStationToEquationStation(out equationStationStr, originalStation))
                    MessageBox.Show("原始桩号转为断链桩号 fail");
                else
                    MessageBox.Show("原始桩号转为断链桩号 ok");
            }

            //获取路基所有横断面
            {

                List<SingleRoadbedTypeSegment> allRoadbedTypeSegment = new List<SingleRoadbedTypeSegment>();
                if (!alignmentInterface.GetRoadbedTypeSegments(allRoadbedTypeSegment) || 0 == allRoadbedTypeSegment.Count)
                    MessageBox.Show("获取路基所有横断面 fail");
                else
                    MessageBox.Show("获取路基所有横断面 ok");
            }

            //获取路线所有断链信息
            {

                List<SzewStationEquationInfo> outStationEquationInfos = new List<SzewStationEquationInfo>();
                if (!alignmentInterface.GetStationEquation(outStationEquationInfos) || 0 == outStationEquationInfos.Count)
                    MessageBox.Show("获取路线所有断链信息 fail");
                else
                    MessageBox.Show("获取路线所有断链信息 ok");
            }

            //计算桩号到平面线起点距离
            {
                string station = "K4+100";
                double stationDistance = alignmentInterface.GetStationDistance(station);
                MessageBox.Show("计算桩号到平面线起点距离 ok");
            }

            //获取平面线元集
            {
                List<SzewSegment> horiSegArrays = new List<SzewSegment>();
                if (!alignmentInterface.GetHoriSegArrays(horiSegArrays) || 0 == horiSegArrays.Count)
                    MessageBox.Show("获取平面线元集 fail");
                else
                    MessageBox.Show("获取平面线元集 ok");
            }

            //获取路线长度（平面线长度）
            {
                double alignmentLength = alignmentInterface.GetAlignmentLength();
                MessageBox.Show("获取路线长度 ok");
            }

            //计算圆曲线外距
            {
                XPoint3d JD = XPoint3d.FromXYZ(0, 0, 0);
                XPoint3d QZ = XPoint3d.FromXYZ(100, 100, 0);
                double dis = alignmentInterface.Calculate(JD, QZ, 100);
                MessageBox.Show("计算圆曲线外距 ok");
            }

            //查询空间点在路面投影点
            {
                XPoint3d projectPt;
                XPoint3d spacePt = XPoint3d.FromXYZ(100000, 100000, 100000);
                if (!alignmentInterface.ProjectPointOnRoad(out projectPt, spacePt))
                    MessageBox.Show("查询空间点在路面投影点 fail");
                else
                    MessageBox.Show("查询空间点在路面投影点 ok");
            }

            //查询空间点在行车道投影点
            {
                XPoint3d projectPt;
                XPoint3d spacePt = XPoint3d.FromXYZ(100000, 100000, 100000);
                if (!alignmentInterface.ProjectPointOnLane(out projectPt, spacePt, true))
                    MessageBox.Show("查询空间点在行车道投影点 fail");
                else
                    MessageBox.Show("查询空间点在行车道投影点 ok");
            }

            //计算路面投影宽度
            {
                double dWidth;
                string dStation = "K3+500";
                RoadbedSectionType roadbedTyoe = RoadbedSectionType.SeparateLeft;
                if (!alignmentInterface.CalculateProjectedWidthOfRoad(out dWidth, dStation, true, roadbedTyoe))
                    MessageBox.Show("计算路面投影宽度 fail");
                else
                    MessageBox.Show("计算路面投影宽度 ok");
            }

            //计算路基两侧边线到中心线距离
            {
                double leftWidth, rightWidth;
                string dStation = "K3+500";
                RoadbedSectionType roadbedTyoe = RoadbedSectionType.SeparateLeft;
                if (!alignmentInterface.CalculateProjectedDistanceOfAlign(out leftWidth, out rightWidth, dStation, roadbedTyoe))
                    MessageBox.Show("计算路基两侧边线到中心线距离 fail");
                else
                    MessageBox.Show("计算路基两侧边线到中心线距离 ok");
            }

            //计算超高旋转轴到路线设计线距离
            {
                double axis;
                string dStation = "K3+500";
                RoadbedSectionType roadbedTyoe = RoadbedSectionType.SeparateLeft;
                if (!alignmentInterface.CalculateSuperElevationRotateAxisOfRoad(out axis, dStation, true, roadbedTyoe))
                    MessageBox.Show("计算超高旋转轴到路线设计线距离 fail");
                else
                    MessageBox.Show("计算超高旋转轴到路线设计线距离 ok");
            }

            //判断指定桩号处是否存在路面模型
            {
                string dStation = "K3+500";
                if (!alignmentInterface.HasRoadSurface(dStation))
                    MessageBox.Show("K3+500桩号处不存在路面模型");
                else
                    MessageBox.Show("K3+500桩号处存在路面模型");
            }

            //造体断面
            {
                List<CrossSectionInfo> crossSectionInfos = new List<CrossSectionInfo>();
                if (!alignmentInterface.GetSectionsCR(crossSectionInfos) || 0 == crossSectionInfos.Count)
                    MessageBox.Show("造体断面 fail");
                else
                    MessageBox.Show("造体断面 ok");
            }

            //获取两个桩号之间中央分隔带信息
            {
                List<XPoint3d> tangentPts = new List<XPoint3d>();
                List<XVector3d> tangentVecs = new List<XVector3d>();
                List<CurveSet> profileCrvs = new List<CurveSet>();

                string startStation = "K1+000";
                string endStation = "K2+000";
                if (!alignmentInterface.GetDividerDataInTwoStation(tangentPts, tangentVecs, profileCrvs,
                    startStation, endStation, 100, true, 0.1))
                    MessageBox.Show("获取两个桩号之间中央分隔带信息 fail");
                else
                    MessageBox.Show("获取两个桩号之间中央分隔带信息 ok");
            }

            //获取两个桩号之间中土路肩信息
            {
                List<XPoint3d> tangentPts = new List<XPoint3d>();
                List<CurveSet> soilShouderCrvs = new List<CurveSet>();
                List<CurveSet> pavementCrvs = new List<CurveSet>();
                List<CurveSet> slopeCrvs = new List<CurveSet>();

                string startStation = "K1+000";
                string endStation = "K2+000";
                if (!alignmentInterface.GetShoulderDataInTwoStation(tangentPts,soilShouderCrvs,pavementCrvs, slopeCrvs,
                    startStation, endStation, 100, true, 0.1))
                    MessageBox.Show("获取两个桩号之间中土路肩信息 fail");
                else
                    MessageBox.Show("获取两个桩号之间中土路肩信息 ok");
            }

            //根据间隔获取桩号
            {
                List<string> stationList = new List<string>();
                double interval = 100000;
                if (!alignmentInterface.GetStationList(stationList, interval) || 0 == stationList.Count)
                    MessageBox.Show("间隔桩号 fail");
                else
                    MessageBox.Show("间隔桩号 ok");
            }
        }
    }
}
